<template>
  <div class="goods-list">
    <router-link class="goods-item" :to="'/home/goodsinfo/' + 6" tag="div">
      <img src="https://img10.360buyimg.com/n7/jfs/t1/4809/12/3501/164097/5b997dd5Eb8a466ef/f04fff1f415df9ec.jpg" alt="">
      <h1 class="title">联想Z5 6GB+128GB 极光蓝联想Z5 6GB+128GB 极光蓝</h1>
      <div class="info">
        <p class="price">
          <span class="now">￥2199</span>
          <span class="old">￥2399</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩余60件</span>
        </p>
      </div>
    </router-link>

    <!-- 在网页中有两种跳转方式 1.使用a标签的形式叫做标签跳转  2. window.location.href的形式, 叫做编程式导航 -->
    <div class="goods-item" @click="getDetail(5)">
      <img src="https://img10.360buyimg.com/n7/jfs/t1/4809/12/3501/164097/5b997dd5Eb8a466ef/f04fff1f415df9ec.jpg" alt="">
      <h1 class="title">联想Z5 6GB+128GB 极光蓝</h1>
      <div class="info">
        <p class="price">
          <span class="now">￥2199</span>
          <span class="old">￥2399</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩余60件</span>
        </p>
      </div>
    </div>

    <div class="goods-item">
      <img src="https://img10.360buyimg.com/n7/jfs/t1/4809/12/3501/164097/5b997dd5Eb8a466ef/f04fff1f415df9ec.jpg" alt="">
      <h1 class="title">联想Z5 6GB+128GB 极光蓝</h1>
      <div class="info">
        <p class="price">
          <span class="now">￥2199</span>
          <span class="old">￥2399</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩余60件</span>
        </p>
      </div>
    </div>

    <div class="goods-item">
      <img src="https://img10.360buyimg.com/n7/jfs/t1/4809/12/3501/164097/5b997dd5Eb8a466ef/f04fff1f415df9ec.jpg" alt="">
      <h1 class="title">联想Z5 6GB+128GB 极光蓝</h1>
      <div class="info">
        <p class="price">
          <span class="now">￥2199</span>
          <span class="old">￥2399</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩余60件</span>
        </p>
      </div>
    </div>

    <div class="goods-item">
      <img src="https://img10.360buyimg.com/n7/jfs/t1/4809/12/3501/164097/5b997dd5Eb8a466ef/f04fff1f415df9ec.jpg" alt="">
      <h1 class="title">联想Z5 6GB+128GB 极光蓝</h1>
      <div class="info">
        <p class="price">
          <span class="now">￥2199</span>
          <span class="old">￥2399</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩余60件</span>
        </p>
      </div>
    </div>

    <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>

    <!-- <router-link class="goods-item" v-for="(item, index) in goodslist" :key="index" :to="'/home/goodslist'+ item.id" tag="div"> 
      <img :src="item.img_url" alt="">
      <h1 class="title">{{item.title}}</h1>
      <div class="info">
        <p class="price">
          <span class="now">￥{{item.sell_price}}</span>
          <span class="old">￥{{item.market_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩余{{item.stock_quantity}}件</span>
        </p>
      </div>
    </router-link> -->
  </div>

  
</template>

<script>
export default {
  data(){   // 往自己组建内部, 挂载一些私有数据的
    return {
      pageIndex: 1,   // 分页的页数
      goodslist: []
    }
  },
  created(){
    // this.getGoodsList()
  },
  methods: {
    getGoodsList(){
      //获取商品列表
      this.$http.get("api/getgoods?pageindex=" + this.pageindex).then(result=>{
        if(result.body.status === 0){
          this.goodslist = this.goodslist.concat(result.body.message)
        }
      })
    },
    getMore(){
      this.pageIndex++
      this.getGoodsList()
    },
    getDetail(id){
      // 使用js的形式进行路由导航

      //区分 this.$route 和 this.$router两个对象
      // this.$route 是路由参数对象, 所有路由中的参数, params,query 都属于它
      // this.$router 是一个路由导航对象, 用它可以方便的 使用 JS 代码, 实现路由的前进、后退、跳转到 URL 地址
      //1. this.$router.push('/home/goodsinfo/' + id)
      //2. this.$router.push({ path: "/home/goodsinfo/"+id})
      this.$router.push({ name: "goodsinfo", params: { id } })
    }
  }
}
</script>

<style lang="scss" scoped>
.goods-list {
  display: flex;
  flex-wrap: wrap;
  padding: 7px;
  justify-content: space-between;

  .goods-item {
    width: 49%;
    border: 1px solid #CCC;
    box-shadow: 0 0 8px #CCC;
    padding: 2px;
    margin: 4px 0;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 293px;
    img {
      width:100%;
    }
    .title {
      font-size: 14px;
    }
    .info {
      background-color: #eee;
      p {
        margin: 0;
        padding: 5px;
      }
      .price{
        .now{
          font-size: 16px;
          font-weight: 400;
          color: red;
        }
        .old{
          text-decoration: line-through;
          margin-left: 10px;
        }
      }
      .sell {
        display: flex;
        justify-content: space-between;
      }
    }
  }
}
</style>
